package com.vivo.tasks;

import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiUserListResponse;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vivo.Utils.DingConfUtil;
import com.vivo.Utils.SendDingDingMsgUtil;
import com.vivo.dingtalk.entity.DingUserInfo;
import com.vivo.dingtalk.service.DingUserInfoService;
import com.vivo.system.entity.VivoDingConf;
import com.vivo.system.service.VivoDingConfService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取钉钉通讯录信息
 */
@Component
public class TaskSyncDingUser {
    private static final Logger log = LoggerFactory.getLogger(TaskSyncDingUser.class);

    @Autowired
    private VivoDingConfService vivoDingConfService;

    @Autowired
    private DingUserInfoService dingUserInfoService;

    @Autowired
    private SystemService systemService;

    @Autowired
    private DingConfUtil dingConfUtil;

    /**
     * 目前只同步总部数据，其他二级数据稍后同步
     */
    public void SyncData() {
        SyncBsData();

        SyncNjData();

//        SyncGysData();
    }


    /**
     * 同步所有总部数据
     */
    public void SyncBsData() {
        String compId = "1";
        String deptId = "1";

        SyncDingUser(compId, deptId);

        log.info("获取本部数据完毕");
    }

    /**
     * 同步南京区域数据
     */
    private void SyncNjData() {
        String compId = "09eb5a01f7cc424c87fca577acd6dd3e";
        String deptId = "1";

        SyncDingUser(compId, deptId);

        log.info("获取南京数据完毕");
    }

    private void SyncGysData() {
        String compId = "2bda83cba6c242e492afbc7af47bd943";
        String deptId = "1";

        SyncDingUser(compId, deptId);

        log.info("获取供应商署姐完毕！");
    }

    private void SyncDingUser(String compId, String deptId) {
        String accessToken = dingConfUtil.getAccessToken(compId);

        List<OapiDepartmentListResponse.Department> departmentList = SendDingDingMsgUtil.getAllDeptList(accessToken, deptId);
        if (departmentList == null) {
            return;
        }

        for (OapiDepartmentListResponse.Department department : departmentList) {
            System.out.println("当前处理部门：" + department.getName());

            // 修改并同步部门信息
            updOfficeInfo(department);

            // 获取部门之下所有员工信息
            List<OapiUserListResponse.Userlist> userList = SendDingDingMsgUtil.getUserDetailList(accessToken, department.getId());
            for (OapiUserListResponse.Userlist dingUser : userList) {
                updDingUserInfo(dingUser, compId);
            }
        }
    }


    /**
     *
     * @param department
     */
    private void updOfficeInfo(OapiDepartmentListResponse.Department department) {
        // TODO 同步组织机构信息
        System.out.println(department.toString());
    }

    private void updDingUserInfo(OapiUserListResponse.Userlist dingUser, String dingCompId) {
        // TODO 同步钉钉登陆信息
        System.out.println(dingUser.toString());

        String mobile = dingUser.getMobile();
        DingUserInfo userInfo = dingUserInfoService.getByUnionid(dingUser.getUnionid());
        if (userInfo == null) {
            userInfo = new DingUserInfo();
        }

        User sysUser = systemService.getUserByLoginName(mobile);
        if (sysUser == null) {
            log.info("用户{}，手机号码{}，还没有在系统进行入职操作！", dingUser.getName(), mobile);
            return;
        }

        /**
         * 在二级的总部用户，不保存数据
         */
        if (!dingCompId.equals("1") && sysUser.getCompany().getId().equals("1")) {
            return;
        }

        userInfo.setUserId(sysUser.getId());
        userInfo.setCompId(sysUser.getCompany().getId());
        userInfo.setDingCompId(dingCompId);
        userInfo.setEmpId(sysUser.getEmployeeId());
        userInfo.setMobile(mobile);
        userInfo.setDingUserId(dingUser.getUserid());
        userInfo.setDingId(dingUser.getDingId());
        userInfo.setPosition(dingUser.getPosition());
        userInfo.setName(sysUser.getName());
        userInfo.setAvatar(dingUser.getAvatar());
        userInfo.setEmail(dingUser.getEmail());
        userInfo.setOpenid(dingUser.getUnionid());
        userInfo.setUnionid(dingUser.getUnionid());
        userInfo.setActive("1");

        try {
            dingUserInfoService.save(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
        }
    }
}
